//
//  HomePageDetailTableViewCell.m
//  InSuger
//
//  Created by dllo on 15/11/28.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "HomePageDetailTableViewCell.h"

@implementation HomePageDetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self create];
    }
    return self;
}

- (void)create {
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    
    self.contentLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.contentLabel];

    self.topImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.topImageView];
    
    self.priceLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.priceLabel];

    self.priceImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.priceImageView];

    self.view = [[UIView alloc] init];
    [self.contentView addSubview:self.view];
    
    self.shareButton = [[UIButton alloc] init];
    [self.contentView addSubview:self.shareButton];

    self.likeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.likeLabel];

    self.likeImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.likeImageView];

    self.buyButton = [[UIButton alloc] init];
    [self.contentView addSubview:self.buyButton];

    self.buyImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.buyImageView];

}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100, CELLWIDTH / 100 * 3, CELLHEIGHT / 100 * 90, CELLWIDTH / 100 * 5));
        
    }];

    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 11, CELLWIDTH / 100 * 3, CELLHEIGHT / 100 * 75, CELLWIDTH / 100 * 5));
        
    }];
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 30, CELLWIDTH / 100 * 3, CELLHEIGHT / 100 * 20, CELLWIDTH / 100 * 5));
        
    }];
    
    [self.priceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 83, CELLWIDTH / 100 * 60, CELLHEIGHT / 100 * 10, CELLWIDTH / 100 * 30));
        
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 83, CELLWIDTH / 100 * 75, CELLHEIGHT / 100 * 10, CELLWIDTH / 100 * 5));
        
    }];
    
    [self.view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 90.7, CELLWIDTH / 100 * 5, CELLHEIGHT / 100 * 9, CELLWIDTH / 100 * 5));
        
    }];
    
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 92, CELLWIDTH / 100 * 5, CELLHEIGHT / 100, CELLWIDTH / 100 * 85));
        
    }];

    [self.likeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 92, CELLWIDTH / 100 * 25, CELLHEIGHT / 100, CELLWIDTH / 100 * 65));
        
    }];

    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 92, CELLWIDTH / 100 * 35, CELLHEIGHT / 100, CELLWIDTH / 100 * 45));
        
    }];
    
    [self.buyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 92, CELLWIDTH / 100 * 60, CELLHEIGHT / 100, CELLWIDTH / 100 * 30));
        
    }];

    [self.buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(CELLHEIGHT / 100 * 92, CELLWIDTH / 100 * 75, CELLHEIGHT / 100, CELLWIDTH / 100 * 5));
        
    }];

}


#pragma mark    用model的set方法为cell赋值
- (void)setModel:(HomePageDetailModel *)model {
    
    if (_model != model) {
        
        _model = model;
        
        self.titleLabel.text = model.title;
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        self.titleLabel.numberOfLines = 0;
        
        self.contentLabel.text = model.desc;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:15];
        
        [self.topImageView sd_setImageWithURL:[NSURL URLWithString:model.pic[0][@"pic"]] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
        
        // 创建点击手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView:)];
        self.topImageView.userInteractionEnabled = YES;
        [self.topImageView addGestureRecognizer:tapGesture];
        
        self.priceLabel.text = [NSString stringWithFormat:@"%@", model.price];
        self.priceImageView.image = [UIImage imageNamed:@"money.png"];
        
        self.view.layer.borderWidth = 1;
        
        [self.shareButton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
        [self.shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
        
        self.likeLabel.text = [NSString stringWithFormat:@"%@", model.likes];
        self.likeImageView.image = [UIImage imageNamed:@"like.png"];
        
        self.buyButton.layer.cornerRadius = 10;
        self.buyButton.layer.borderWidth = 1;
        [self.buyButton setTitle:@"购买" forState:UIControlStateNormal];

        [self.buyButton addTarget:self action:@selector(shopping) forControlEvents:UIControlEventTouchUpInside];
        
        self.buyImageView.image = [UIImage imageNamed:@"buying.png"];
    }
    
    @weakify(self);
    [self addColorChangedBlock:^() {
        @strongify(self);
        self.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.normalBackgroundColor = [UIColor whiteColor];
        self.contentView.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.contentView.normalBackgroundColor = [UIColor whiteColor];
        self.titleLabel.nightTextColor = [UIColor whiteColor];
        self.contentLabel.nightTextColor = [UIColor whiteColor];
        self.priceLabel.nightTextColor = [UIColor whiteColor];
        self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.shareButton.nightTitleColor = [UIColor whiteColor];
        self.likeLabel.nightTextColor = [UIColor whiteColor];
        self.buyButton.nightTitleColor = [UIColor whiteColor];
        self.buyButton.normalTitleColor = [UIColor redColor];
    }];
    
}
- (void)clickImageView:(UITapGestureRecognizer *)tap{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否保存图片" message:@"保存吗?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    alert.tag = 3000;
    [alert show];

}
//根据被点击按钮的索引处理点击事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 3000) {
        if (buttonIndex == 0) {
            [self saveImageToPhotos:self.topImageView.image];
        } else{
            
        }
    }
}

- (void)saveImageToPhotos:(UIImage*)savedImage

{
    UIImageWriteToSavedPhotosAlbum(self.topImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存图片失败" ;
        NSLog(@"%@", error);
    }else{
        msg = @"保存图片成功" ;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

- (void)shareAction {

    HomePageDetailViewController *detailVC = [[HomePageDetailViewController alloc] init];
    detailVC.detailModel = _model;
    [self.delegate share:detailVC.detailModel];
    
}


- (void)shopping {
    
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
